﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AbacusCustom
{
    public partial class frmManageReport : Form
    {
        public frmManageReport()
        {
            InitializeComponent();
        }

        private void get_report_list()
        {
            Module db_conf = new Module();
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString =
            "Data Source=" + db_conf.db_host + ";" +
            "Database=PSCUSTOM;" +
            "User ID=" + db_conf.db_user + ";" +
            "Password=" + db_conf.db_password + ";";
            conn.Open();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM PS_REPORT WHERE ACCOUNT_ID='" + Module.session_userid + "' ORDER BY ID ASC";
            dgvReportSelection.Rows.Clear();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvReportSelection);
                row.Cells[0].Value = reader.GetValue(0).ToString();
                row.Cells[1].Value = reader.GetValue(2).ToString();
                row.Cells[2].Value = reader.GetValue(3).ToString();
                dgvReportSelection.Rows.Add(row);
            }

            conn.Close();
        }

        private void get_report_column(String report_id)
        {
            Module db_conf = new Module();
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString =
            "Data Source=" + db_conf.db_host + ";" +
            "Database=PSCUSTOM;" +
            "User ID=" + db_conf.db_user + ";" +
            "Password=" + db_conf.db_password + ";";
            conn.Open();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT a.ID,REPORT_ID,c.NAME,d.NAME,b.NAME,a.NAME,a.DESCRIPTION FROM PS_REPORT_COLUMN a JOIN PS_COLUMN b ON a.COLUMN_ID=b.ID JOIN PS_TABLE_TYPE c ON a.TABLE_TYPE_ID = c.ID JOIN PS_TABLE d ON a.TABLE_ID = d.ID WHERE REPORT_ID='" + report_id + "' ORDER BY a.ID ASC";
            dgvColumnList.Rows.Clear();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvColumnList);
                row.Cells[0].Value = reader.GetValue(0).ToString();
                row.Cells[1].Value = reader.GetValue(1).ToString();
                row.Cells[2].Value = reader.GetValue(2).ToString();
                row.Cells[3].Value = reader.GetValue(3).ToString();
                row.Cells[4].Value = reader.GetValue(4).ToString();
                row.Cells[5].Value = reader.GetValue(5).ToString();
                row.Cells[6].Value = reader.GetValue(6).ToString();
                dgvColumnList.Rows.Add(row);
            }

            conn.Close();
        }

        private void dgvReportSelection_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                get_report_column(dgvReportSelection.Rows[e.RowIndex].Cells[0].Value.ToString());
            }
            catch (Exception a)
            {
                MessageBox.Show(a.ToString());
            }
        }

        private void frmManageReport_Load(object sender, EventArgs e)
        {
            try
            {
                get_report_list();
            }
            catch (Exception)
            {

            }
        }


    }
}
